package com.example.demo.controller.equipment;

import com.example.demo.domain.equipment.Equipment;
import com.example.demo.service.equipment.EquipmentService;
import net.sf.json.JSONArray;
import net.sf.json.JSONObject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.List;

@RestController
@RequestMapping
@CrossOrigin
public class EquipmentController {
    @Resource
    @Autowired
    private EquipmentService equipmentService;
    private List<Equipment> equipmentList;

    @PostMapping(value = "/add-equipment")
    public Equipment addEquipment(@RequestBody Equipment equipment) {
        return equipmentService.addequip(equipment);
    }

    @GetMapping(value = "/get-plan-id")
    public void getPlanId(@RequestBody Equipment equipment) {
        equipmentService.getPlanid(equipment);
    }

    @GetMapping(value = "/equipment-list")
    public String getEquipment(@RequestParam("nowpage")Integer nowpage, @RequestParam("pagesize")Integer pagesize,
                               @RequestParam("select")String select, @RequestParam("searchContent")String searchContent){
        equipmentList=equipmentService.getEquipment();
        JSONArray jsonArray = new JSONArray();
        JSONObject jsonObject=new JSONObject();
        jsonObject.put("number",equipmentList.size());
        jsonArray.add(jsonObject);
        List<Equipment> equipments = equipmentList.subList((nowpage - 1) * pagesize,Math.min( nowpage * pagesize,equipmentList.size()));
        for (Equipment temp:equipments){
            JSONObject jsonObject1=new JSONObject();
            jsonObject1.put("ticketId",temp.getTicketId());
            jsonObject1.put("planId",temp.getPlanId());
            jsonObject1.put("equipmentName",temp.getEquipmentName());
            jsonObject1.put("tbName",temp.getTbName());
            jsonObject1.put("tbDepartment",temp.getTbDepartment());
            jsonObject1.put("date",temp.getDate());
            jsonObject1.put("fzName",temp.getFzName());
            jsonObject1.put("fzDepartment",temp.getFzDepartment());
            jsonObject1.put("state",temp.getState());
            jsonArray.add(jsonObject1);
        }
        return jsonArray.toString();
    }

    //返回已通过列表
    @GetMapping(value = "/pass-equipment-list")
    public String getPassedEquipment(@RequestParam("nowpage")Integer nowpage, @RequestParam("pagesize")Integer pagesize,
                                     @RequestParam("select")String select, @RequestParam("searchContent")String searchContent){
        equipmentList=equipmentService.getPassEquipment();
        JSONArray jsonArray = new JSONArray();
        JSONObject jsonObject=new JSONObject();
        jsonObject.put("number",equipmentList.size());
        jsonArray.add(jsonObject);
        List<Equipment> equipments = equipmentList.subList((nowpage - 1) * pagesize,Math.min( nowpage * pagesize,equipmentList.size()));
        for (Equipment temp:equipments){
            JSONObject jsonObject1=new JSONObject();
            jsonObject1.put("ticketId",temp.getTicketId());
            jsonObject1.put("planId",temp.getPlanId());
            jsonObject1.put("equipmentName",temp.getEquipmentName());
            jsonObject1.put("tbName",temp.getTbName());
            jsonObject1.put("tbDepartment",temp.getTbDepartment());
            jsonObject1.put("date",temp.getDate());
            jsonObject1.put("fzName",temp.getFzName());
            jsonObject1.put("fzDepartment",temp.getFzDepartment());
            jsonObject1.put("state",temp.getState());
            jsonArray.add(jsonObject1);
        }
        return jsonArray.toString();
    }

    //返回未通过列表
    @GetMapping(value = "/reject-equipment-list")
    public String getRejectedEquipment(@RequestParam("nowpage")Integer nowpage, @RequestParam("pagesize")Integer pagesize,
                                       @RequestParam("select")String select, @RequestParam("searchContent")String searchContent){
        equipmentList=equipmentService.getRejectEquipment();
        JSONArray jsonArray = new JSONArray();
        JSONObject jsonObject=new JSONObject();
        jsonObject.put("number",equipmentList.size());
        jsonArray.add(jsonObject);
        List<Equipment> equipments = equipmentList.subList((nowpage - 1) * pagesize,Math.min( nowpage * pagesize,equipmentList.size()));
        for (Equipment temp:equipments){
            JSONObject jsonObject1=new JSONObject();
            jsonObject1.put("ticketId",temp.getTicketId());
            jsonObject1.put("planId",temp.getPlanId());
            jsonObject1.put("equipmentName",temp.getEquipmentName());
            jsonObject1.put("tbName",temp.getTbName());
            jsonObject1.put("tbDepartment",temp.getTbDepartment());
            jsonObject1.put("date",temp.getDate());
            jsonObject1.put("fzName",temp.getFzName());
            jsonObject1.put("fzDepartment",temp.getFzDepartment());
            jsonObject1.put("state",temp.getState());
            jsonArray.add(jsonObject1);
        }
        return jsonArray.toString();
    }

    //返回未审批列表
    @GetMapping(value = "/wait-equipment-list")
    public String getWaitEquipment(@RequestParam("nowpage")Integer nowpage, @RequestParam("pagesize")Integer pagesize,
                                   @RequestParam("select")String select, @RequestParam("searchContent")String searchContent){
        equipmentList=equipmentService.getWaitEquipment();
        JSONArray jsonArray = new JSONArray();
        JSONObject jsonObject=new JSONObject();
        jsonObject.put("number",equipmentList.size());
        jsonArray.add(jsonObject);
        List<Equipment> equipments = equipmentList.subList((nowpage - 1) * pagesize,Math.min( nowpage * pagesize,equipmentList.size()));
        for (Equipment temp:equipments){
            JSONObject jsonObject1=new JSONObject();
            jsonObject1.put("ticketId",temp.getTicketId());
            jsonObject1.put("planId",temp.getPlanId());
            jsonObject1.put("equipmentName",temp.getEquipmentName());
            jsonObject1.put("tbName",temp.getTbName());
            jsonObject1.put("tbDepartment",temp.getTbDepartment());
            jsonObject1.put("date",temp.getDate());
            jsonObject1.put("fzName",temp.getFzName());
            jsonObject1.put("fzDepartment",temp.getFzDepartment());
            jsonObject1.put("state",temp.getState());
            jsonArray.add(jsonObject1);
        }
        return jsonArray.toString();
    }

    @PutMapping(value = "/pass-equipment")
    public Equipment pass(@RequestBody Equipment equipment){
        System.out.print(equipment);
        return equipmentService.passEquipment(equipment);
    }

    @PutMapping(value = "/reject-equipment")
    public Equipment reject(@RequestBody Equipment equipment){
        System.out.print(equipment);
        return equipmentService.rejectEquipment(equipment);
    }
}
